// AIPlugin.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include "AIPlugin.h"
#include "..\Shared\ObjectManager.h"

__declspec(dllexport) IPlugin* __stdcall CreateInstance() // needed for pluginmanager
{
	return (IPlugin*)new AIPlugin(); // returns a newly created instance of the AIPlugin
}

__declspec(dllexport) int __stdcall GetInterfaceVersion(void) // Needed for pluginmanager
{
	return PluginInterfaceVersion; // returns the Plugin Interface Version. 
}

AIPlugin::AIPlugin()
{
	m_ModuleInfo.pluginName = "AIPlugin"; // Change to the name of your plugin
	m_ModuleInfo.author = "Mike Pierce & Craig Lynch"; // Change to your name
	m_ModuleInfo.version = 0x01000000; // 0xMMmmaabb   MM = Major mm = Minor aa = Macro bb = Build
	m_ModuleInfo.numOfCommands = 1; // Needs to equal the number of commands that your plugin handles.
	m_ModuleInfo.commands[0] = "EndTurn"; // Each of these is the name of the command.
}

bool __stdcall AIPlugin::HandleCommand(Command &NewCommand)
{
	bool Found = false;
	for(int i = 0; i < m_ModuleInfo.numOfCommands; i++) // Checking all handled commands against the supply command
	{
		if(m_ModuleInfo.commands[i] == NewCommand.name) // If a match of names we have found and break out of the loop
		{
			Found = true;
			break;
		}
	}
	if(!Found) // if not found return false to the plugin manager for this command.
		return false;
	if(NewCommand.name == "EndTurn") // Here is handle the Printf command
	{
		int charX, charY;
		//find character from object manager

		Container<Object*> players;//= m_ObjectManager.GetObjectsWithAttribute("PC", "True");
		//set charX and charY to character's x and y values
		charX = players[0].GetBaseAttribute("X").ToInt();
		charY = players[0].GetBaseAttribute("Y").ToInt();
		//find all enemies from object manager
		Container<Object*> enemies;//=m_ObjectManager.GetObjectsWithAttribute("PC", "False");
		//create for loop that:
		for (int i = 0; i < enemies.Size(); ++i)
		{
			//while (monster's speed remains)
			int speedRemaining = enemies[i].GetBaseAttribute("Speed").ToInt();
			do
			{
				int enemyX = enemies[i].GetBaseAttribute("X").ToInt();
				int enemyY = enemies[i].GetBaseAttribute("Y").ToInt();
				//check to see if monster is in range to attack
				if ((enemyX - charX <= 1)&&(enemyX - charX >= -1)&&(enemyY - charY <= 1)&&(enemyY - charY >= -1))// does not need to change Y value
				{
					Command Attack;
					Attack.name = "Attack";
					Attack.Arguments.Add(players[0].GetBaseAttribute("Name"));
					Attack.Arguments.Add(enemies[i].GetBaseAttribute("Name"));
					m_PluginManager.HandleCommand(Attack);
					speedRemaining = 0;
				}
				else
				{
					Command move;
					move.name = "Move";
					if (enemyX < charX) // move enemy to left
						move.Arguments.Add(Value("X", -1));
					if (enemyX > charX) // move enemy to right
						move.Arguments.Add(Value("X", 1));
					if (enemyY < charY) // move enemy down
						move.Arguments.Add(Value("Y", -1));
					if (enemyY > charY) // move enemy up
						move.Arguments.Add(Value("X", 1));
					m_Manager->HandleCommand(move);
				}
						speedRemaining -= 5;
			}while(speedRemaining > 0);
		}
			//if not, move closer to the enemy

		return true; // returns that the command was handled.
	}
	return false;
}